<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span>
</span><span id="line-2"></span><span class="hs-comment">-- | The assignment of virtual registers to stack slots</span><span>
</span><span id="line-3"></span><span>
</span><span id="line-4"></span><span class="hs-comment">--      We have lots of stack slots. Memory-to-memory moves are a pain on most</span><span>
</span><span id="line-5"></span><span class="hs-comment">--      architectures. Therefore, we avoid having to generate memory-to-memory moves</span><span>
</span><span id="line-6"></span><span class="hs-comment">--      by simply giving every virtual register its own stack slot.</span><span>
</span><span id="line-7"></span><span>
</span><span id="line-8"></span><span class="hs-comment">--      The StackMap stack map keeps track of virtual register - stack slot</span><span>
</span><span id="line-9"></span><span class="hs-comment">--      associations and of which stack slots are still free. Once it has been</span><span>
</span><span id="line-10"></span><span class="hs-comment">--      associated, a stack slot is never &quot;freed&quot; or removed from the StackMap again,</span><span>
</span><span id="line-11"></span><span class="hs-comment">--      it remains associated until we are done with the current CmmProc.</span><span>
</span><span id="line-12"></span><span class="hs-comment">--</span><span>
</span><span id="line-13"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">GHC.CmmToAsm.Reg.Linear.StackMap</span><span> </span><span class="hs-special">(</span><span>
</span><span id="line-14"></span><span>        </span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackSlot"><span class="hs-identifier">StackSlot</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-15"></span><span>        </span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier">StackMap</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-16"></span><span>        </span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#emptyStackMap"><span class="hs-identifier">emptyStackMap</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-17"></span><span>        </span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#getStackSlotFor"><span class="hs-identifier">getStackSlotFor</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-18"></span><span>        </span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#getStackUse"><span class="hs-identifier">getStackUse</span></a></span><span>
</span><span id="line-19"></span><span class="hs-special">)</span><span>
</span><span id="line-20"></span><span>
</span><span id="line-21"></span><span class="hs-keyword">where</span><span>
</span><span id="line-22"></span><span>
</span><span id="line-23"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Prelude.html"><span class="hs-identifier">GHC.Prelude</span></a></span><span>
</span><span id="line-24"></span><span>
</span><span id="line-25"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Types.Unique.FM.html"><span class="hs-identifier">GHC.Types.Unique.FM</span></a></span><span>
</span><span id="line-26"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Types.Unique.html"><span class="hs-identifier">GHC.Types.Unique</span></a></span><span>
</span><span id="line-27"></span><span>
</span><span id="line-28"></span><span>
</span><span id="line-29"></span><span class="hs-comment">-- | Identifier for a stack slot.</span><span>
</span><span id="line-30"></span><span class="hs-keyword">type</span><span> </span><span id="StackSlot"><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackSlot"><span class="hs-identifier hs-var">StackSlot</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="hs-identifier hs-type">Int</span></span><span>
</span><span id="line-31"></span><span>
</span><span id="line-32"></span><span class="hs-keyword">data</span><span> </span><span id="StackMap"><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier hs-var">StackMap</span></a></span></span><span>
</span><span id="line-33"></span><span>        </span><span class="hs-glyph">=</span><span> </span><span id="StackMap"><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier hs-var">StackMap</span></a></span></span><span>
</span><span id="line-34"></span><span>        </span><span class="hs-special">{</span><span> </span><span class="hs-comment">-- | The slots that are still available to be allocated.</span><span>
</span><span id="line-35"></span><span>          </span><span id="stackMapNextFreeSlot"><span class="annot"><span class="annottext">StackMap -&gt; Int
</span><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#stackMapNextFreeSlot"><span class="hs-identifier hs-var hs-var">stackMapNextFreeSlot</span></a></span></span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="hs-glyph">!</span><span class="annot"><span class="hs-identifier hs-type">Int</span></span><span>
</span><span id="line-36"></span><span>
</span><span id="line-37"></span><span>          </span><span class="hs-comment">-- See Note [UniqFM and the register allocator]</span><span>
</span><span id="line-38"></span><span>          </span><span class="hs-comment">-- | Assignment of vregs to stack slots.</span><span>
</span><span id="line-39"></span><span>        </span><span class="hs-special">,</span><span> </span><span id="stackMapAssignment"><span class="annot"><span class="annottext">StackMap -&gt; UniqFM Unique Int
</span><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#stackMapAssignment"><span class="hs-identifier hs-var hs-var">stackMapAssignment</span></a></span></span><span>    </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Types.Unique.FM.html#UniqFM"><span class="hs-identifier hs-type">UniqFM</span></a></span><span> </span><span class="annot"><a href="GHC.Types.Unique.html#Unique"><span class="hs-identifier hs-type">Unique</span></a></span><span> </span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackSlot"><span class="hs-identifier hs-type">StackSlot</span></a></span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-40"></span><span>
</span><span id="line-41"></span><span>
</span><span id="line-42"></span><span class="hs-comment">-- | An empty stack map, with all slots available.</span><span>
</span><span id="line-43"></span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#emptyStackMap"><span class="hs-identifier hs-type">emptyStackMap</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier hs-type">StackMap</span></a></span><span>
</span><span id="line-44"></span><span id="emptyStackMap"><span class="annot"><span class="annottext">emptyStackMap :: StackMap
</span><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#emptyStackMap"><span class="hs-identifier hs-var hs-var">emptyStackMap</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Int -&gt; UniqFM Unique Int -&gt; StackMap
</span><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier hs-var">StackMap</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="annot"><span class="annottext">UniqFM Unique Int
forall key elt. UniqFM key elt
</span><a href="GHC.Types.Unique.FM.html#emptyUFM"><span class="hs-identifier hs-var">emptyUFM</span></a></span><span>
</span><span id="line-45"></span><span>
</span><span id="line-46"></span><span>
</span><span id="line-47"></span><span class="hs-comment">-- | If this vreg unique already has a stack assignment then return the slot number,</span><span>
</span><span id="line-48"></span><span class="hs-comment">--      otherwise allocate a new slot, and update the map.</span><span>
</span><span id="line-49"></span><span class="hs-comment">--</span><span>
</span><span id="line-50"></span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#getStackSlotFor"><span class="hs-identifier hs-type">getStackSlotFor</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier hs-type">StackMap</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Types.Unique.html#Unique"><span class="hs-identifier hs-type">Unique</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier hs-type">StackMap</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="hs-identifier hs-type">Int</span></span><span class="hs-special">)</span><span>
</span><span id="line-51"></span><span>
</span><span id="line-52"></span><span id="getStackSlotFor"><span class="annot"><span class="annottext">getStackSlotFor :: StackMap -&gt; Unique -&gt; (StackMap, Int)
</span><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#getStackSlotFor"><span class="hs-identifier hs-var hs-var">getStackSlotFor</span></a></span></span><span> </span><span id="local-6989586621680941631"><span class="annot"><span class="annottext">fs :: StackMap
</span><a href="#local-6989586621680941631"><span class="hs-identifier hs-var">fs</span></a></span></span><span class="hs-glyph">@</span><span class="hs-special">(</span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier hs-type">StackMap</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621680941630"><span class="annot"><span class="annottext">UniqFM Unique Int
</span><a href="#local-6989586621680941630"><span class="hs-identifier hs-var">reserved</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621680941629"><span class="annot"><span class="annottext">Unique
</span><a href="#local-6989586621680941629"><span class="hs-identifier hs-var">reg</span></a></span></span><span>
</span><span id="line-53"></span><span>  </span><span class="hs-glyph">|</span><span> </span><span class="annot"><a href="../../base/src/GHC.Maybe.html#Just"><span class="hs-identifier hs-type">Just</span></a></span><span> </span><span id="local-6989586621680941628"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621680941628"><span class="hs-identifier hs-var">slot</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">UniqFM Unique Int -&gt; Unique -&gt; Maybe Int
forall key elt. Uniquable key =&gt; UniqFM key elt -&gt; key -&gt; Maybe elt
</span><a href="GHC.Types.Unique.FM.html#lookupUFM"><span class="hs-identifier hs-var">lookupUFM</span></a></span><span> </span><span class="annot"><span class="annottext">UniqFM Unique Int
</span><a href="#local-6989586621680941630"><span class="hs-identifier hs-var">reserved</span></a></span><span> </span><span class="annot"><span class="annottext">Unique
</span><a href="#local-6989586621680941629"><span class="hs-identifier hs-var">reg</span></a></span><span>  </span><span class="hs-glyph">=</span><span>  </span><span class="hs-special">(</span><span class="annot"><span class="annottext">StackMap
</span><a href="#local-6989586621680941631"><span class="hs-identifier hs-var">fs</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621680941628"><span class="hs-identifier hs-var">slot</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-54"></span><span>
</span><span id="line-55"></span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#getStackSlotFor"><span class="hs-identifier hs-var">getStackSlotFor</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier hs-type">StackMap</span></a></span><span> </span><span id="local-6989586621680941626"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621680941626"><span class="hs-identifier hs-var">freeSlot</span></a></span></span><span> </span><span id="local-6989586621680941625"><span class="annot"><span class="annottext">UniqFM Unique Int
</span><a href="#local-6989586621680941625"><span class="hs-identifier hs-var">reserved</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621680941624"><span class="annot"><span class="annottext">Unique
</span><a href="#local-6989586621680941624"><span class="hs-identifier hs-var">reg</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-56"></span><span>    </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int -&gt; UniqFM Unique Int -&gt; StackMap
</span><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier hs-var">StackMap</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621680941626"><span class="hs-identifier hs-var">freeSlot</span></a></span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="../../base/src/GHC.Num.html#%2B"><span class="hs-operator hs-var">+</span></a></span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">1</span></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">UniqFM Unique Int -&gt; Unique -&gt; Int -&gt; UniqFM Unique Int
forall key elt.
Uniquable key =&gt;
UniqFM key elt -&gt; key -&gt; elt -&gt; UniqFM key elt
</span><a href="GHC.Types.Unique.FM.html#addToUFM"><span class="hs-identifier hs-var">addToUFM</span></a></span><span> </span><span class="annot"><span class="annottext">UniqFM Unique Int
</span><a href="#local-6989586621680941625"><span class="hs-identifier hs-var">reserved</span></a></span><span> </span><span class="annot"><span class="annottext">Unique
</span><a href="#local-6989586621680941624"><span class="hs-identifier hs-var">reg</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621680941626"><span class="hs-identifier hs-var">freeSlot</span></a></span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621680941626"><span class="hs-identifier hs-var">freeSlot</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-57"></span><span>
</span><span id="line-58"></span><span class="hs-comment">-- | Return the number of stack slots that were allocated</span><span>
</span><span id="line-59"></span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#getStackUse"><span class="hs-identifier hs-type">getStackUse</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier hs-type">StackMap</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="hs-identifier hs-type">Int</span></span><span>
</span><span id="line-60"></span><span id="getStackUse"><span class="annot"><span class="annottext">getStackUse :: StackMap -&gt; Int
</span><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#getStackUse"><span class="hs-identifier hs-var hs-var">getStackUse</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.CmmToAsm.Reg.Linear.StackMap.html#StackMap"><span class="hs-identifier hs-type">StackMap</span></a></span><span> </span><span id="local-6989586621680941621"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621680941621"><span class="hs-identifier hs-var">freeSlot</span></a></span></span><span> </span><span class="annot"><span class="annottext">UniqFM Unique Int
</span><span class="hs-identifier">_</span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621680941621"><span class="hs-identifier hs-var">freeSlot</span></a></span><span>
</span><span id="line-61"></span><span>
</span><span id="line-62"></span></pre></body></html>